var report=new String();
report = "";
var bFiltrateParam =true; //for replaceString method to filtrate the 'font' format;

//basic functions
function isEmpty(s)	//Is the string s empty? Empty:return true.
{
	return ((s == null) || (s.length == 0) || (s == "") || (Trim(s)).length == 0);
}

function hasSpace(s)	//Does the string s have space character? Has:return true.
{
	var whitespace = " \t\n\r";
	var i,c;
	for (i = 0; i < s.length; i++)
	{
		c = s.charAt(i);
		if (whitespace.indexOf(c) >= 0)	return true;
	}
	return false;
}

function isAllSpace(s)	//Is the string s all space? All space:return true.
{
	var i,t;
	if(s==null) return true;
	t="";
	for(i = 0; i < s.length; i++)
		t= t + " ";
	if(t==s)
		return true;
	else
		return false;
}

function isAllZero(s)	//Is the string s all 0? All 0:return true.
{
	var i,t;
	if(s==null) return true;
	t="";
	for(i = 0; i < s.length; i++)
		t= t + "0";
	if(t==s)
		return true;
	else
		return false;
}

function isAllChinese(s)	//Is the string s all Chinese? All Chinese:return true.
{
	return true;
}

function isAllEnglish(s)	//Is the string s all English? All English:return true.
{
	var i,c;
	for(i=0;i<s.length;i++)
	{
		c=s.charAt(i);
		if((c>="A"&&c<="Z")||(c>="a"&&c<="z")||c=="_"||(c>=0&&c<=9))
		{
			if(c==" ")
				return false;
			continue;
		}
		else
			return false;
	}
	return true;
}



function isCharsInBag(s, bag)	//If the characters in the string s are all in the string bag,return true.
{
	var i,c;
	for (i = 0; i < s.length; i++)
	{
		c = s.charAt(i);
		if (bag.indexOf(c) == -1) return false;
	}
	return true;
}

function isCharsInBagEx(s, bag)	//If there is a charater in the string s also in the string bag,return the character.
{
	var i,c;
	for (i = 0; i < s.length; i++)
	{
		c = s.charAt(i);
		if (bag.indexOf(c) > -1) return c;
	}
	return "";
}

function BadCharsInBag(s,bag)	//If there is a charater in the string s also in the string bag,return false.
{
	var i,c;
	for (i = 0; i < s.length; i++)
	{
		c = s.charAt(i);
		if (bag.indexOf(c) > -1) return false;
	}
	return true;
}

//function about the date select box
//function additem(daysize,day)
function additem(thisform,daysize,day)
{
	var i;
	for(i=1;i<=daysize;i++)
	{
	        option=document.createElement ("option");
		option.value=i;
		option.text=i;
		thisform.day.add(option);
	}
	if(day>daysize)
		thisform.day.value=daysize;
	else
		thisform.day.value=day;
}

function setday(thisform)
{
	var i,year,month,day;

	year=parseInt(thisform.year.value);
	month=parseInt(thisform.month.value);
	day=parseInt(thisform.day.value);
	for(i=thisform.day.length;i>=0;i--)
		thisform.day.remove(i);
	switch(month)
	{
		case 1:
		case 3:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			additem(thisform,31,day);
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			additem(30,day);
			break;
		case 2:
			if(!(year%4))
				if(!(year%400))
					additem(thisform,29,day);
				else if(year%100)
					additem(thisform,29,day);
				else
					additem(thisform,28,day);
			else
					additem(thisform,28,day);
			break;
	}
	return true;
}

function inittime(thisform,back_or_forward)
{
	for(var i=1;i<=12;i++)
	{
		var opt=document.createElement ("option");
		opt.value=i;
		opt.text=i;
		thisform.month.add(opt);
	}
	thisform.month.value = 1;
	for(var i=1;i<=31;i++)
	{
		var opt=document.createElement ("option");
		opt.value=i;
		opt.text=i;
		thisform.day.add(opt);
	}
	thisform.day.value = 1;
	var date=new Date();
	var year1=date.getYear();
	if(year1<1900)
		year1=1900+year1;
	if(back_or_forward==0){
		for(var i=year1-100;i<=year1;i++)
		{
			var opt=document.createElement ("option");
			opt.value=i;
			opt.text=i;
			thisform.year.add(opt);
		}
		thisform.year.value = 1980;
	}else if(back_or_forward==1){
		for(var i=year1;i<=year1+100;i++)
		{
			var opt=document.createElement ("option");
			opt.value=i;
			opt.text=i;
			thisform.year.add(opt);
		}
		thisform.year.value = 2001;
	}
}

//validation functions
function isIDCardNumber(s, canNull)	//1.Is the ID Card number valid?
{
	var bag = "0123456789";

	if(isEmpty(s)){
		if(canNull == false){
			report=report+"??????????\n";
			return false;
		}else
			return true;
	}
	if ((s.length!=15)&&(s.length!=18))
	{
		report=report+"?????????????????????\n";
		return false;
	}
  	if (!isCharsInBag(s, bag))
	{
		report=report+"?????????????????????\n";
		return false;
	}
	return true;
}

function isZipCode(s,canNull,zipType)	//3.Is the zipcode valid?
{
	if(isEmpty(s)){
		if(canNull == false){
			report=report+zipType+"?????????\n";
			return false;
		}else
			return true;
	}
	if (s.length!=6)
	{
		report=report+zipType+"??????????\n";
		return false;
	}
	if (!isCharsInBag(s,"0123456789"))
	{
		report=report+zipType+"??????????\n";
		return false;
	}
	return true;
}

function isTel(s,canNull,minl,maxl,telType)	//4.Is the telephone number valid?
{
	var bag = "0123456789-()*";

	if(isEmpty(s)){
		if(canNull == false){
			report=report+telType+"?????\n";
			return false;
		}else
			return true;
	}
	if ((s.length > maxl)||(s.length < minl))
	{
		report=report+telType+"????"+minl+"??????"+maxl+"??\n";
		return false;
	}
	if (!isCharsInBag(s, bag))
	{
		report=report+"???"+telType+"????????\n"+bag+"\n";
		return false;
	}
	return true;
}

function isAddress(s,canNull,minl,maxl,addrType)	//5.Is the address valid?
{
	if(isEmpty(s)){
		if(canNull == false){
			report=report+addrType+"???????\n";
			return false;
		}else
			return true;
	}
	if ((s.length > maxl)||(s.length < minl))
	{
		report=report+addrType+"??????"+minl+"??????"+maxl+"??\n";
		return false;
	}
	if(isAllSpace(s))
	{
		report=report+addrType+"?????????";
		return false;
	}
	return true;
}

function isName(s,canNull,minl,maxl,nameType)	//6.Is the name valid?
{
	if(isEmpty(s)){
		if(canNull == false){
			report=report+nameType+"?????\n";
			return false;
		}else
			return true;
	}
	if((s.length > maxl)||(s.length < minl))
	{
		report=report+nameType+"????"+minl+"??????"+maxl+"??\n";
		return false;
	}
	if(isAllSpace(s))
	{
		report=report+nameType+"???????";
		return false;
	}
	return true;
}

function isAccount(s,canNull,minl,maxl,accType)	//7.Is the account number valid?
{
	var bag = "0123456789";

	if(isEmpty(s)){
		if(canNull == false){
			report=report+accType+"???????\n";
			return false;
		}else
			return true;
	}
	if ((s.length > maxl)||(s.length < minl))
	{
		report=report+"??????"+minl+"??????"+maxl+"??\n";
		return false;
	}
	if (!isCharsInBag(s, bag))
	{
		report=report+"???"+accType+"??????????\n"+bag+"\n";
		return false;
	}
	return true;
}

function isMoney(s,canNull,minamount,maxamount,moneyType)	//8.Is the money valid?
{
	var temp,pos,ad;
	if(isEmpty(s)){
		if(canNull == false){
			report=report+moneyType+"?????\n";
			return false;
		}else
			return true;
	}
	temp=parseFloat(s);
	pos=s.indexOf(".");
	ad=s.substring(pos+1,s.length);
	//alert("temp="+temp+",pos="+pos+",ad="+ad+",s.length="+s.length+",temp.length="+(""+temp).length);
	if(isNaN(temp)==true)
	{
		report=report+moneyType + "??????\n";
		return false;
	}
	if(temp<=0)
	{
		report=report+moneyType + "??????\n";
		return false;
	}
	if((temp<minamount)||(temp>maxamount))
	{
		report=report+moneyType+"????"+minamount+"??????"+maxamount+"??\n";
		return false;
	}
	if(s.length!=(""+temp).length)
	{
		if(isAllZero(ad)&&!isEmpty(ad))
			return true;
		else{
			report=report+moneyType + "?????????\n";
			return false;
		}
	}
	if(pos>0&&ad.length>2)
	{
		report=report+moneyType + "????????2????\n";
		return false;
	}
	return true;
}

function isPassword(s,canNull,minl,maxl,pwdType)	//9.Is the password valid?
{
	if(isEmpty(s)){
		if(canNull == false){
			report=report+pwdType+"?????\n";
			return false;
		}else
			return true;
	}
	if ((s.length > maxl)||(s.length < minl))
	{
		report=report+pwdType+"????"+minl+"??????"+maxl+"??\n";
		return false;
	}
	return true;
}

function isUserid(s,canNull,minl,maxl)	//10.Is the userid valid?
{
 	var errorChar;
	var badChar = "><,[]{}?/+=|\\'\":;~!#$%()`";
	if(isEmpty(s)){
		if(canNull == false){
			report=report+"????????\n";
			return false;
		}else
			return true;
	}
	if ((s.length > maxl)||(s.length < minl))
	{
		report=report+"???????"+minl+"??????"+maxl+"??\n";
		return false;
	}
   	if (hasSpace(s))
	{
		report=report+"?????????????????????\n";
		return false;
	}
	errorChar = isCharsInBagEx(s, badChar)
    	if (errorChar != "")
	{
		report=report+"???????"+s+"???????,????????????"+errorChar+"?????????????\n";
		return false;
	}
	return true;
}

function ComparePwd(pwd1,pwd2)	//11.Are the two password same?
{
	if(s1!=s2)
	{
		report=report+"??????????????\n";
		return false;
	}
	return true;
}

function isPeriod(s,canNull,min,max,prdType)	//12.Is the period of validity valid?
{
	var temp;
	if(isEmpty(s)){
		if(canNull == false){
			report=report+prdType+"?????\n";
			return false;
		}else
			return true;
	}
	temp=parseInt(s);
	if(isNaN(temp)==true)
	{
		report=report+prdType + "??????\n";
		return false;
	}
	if(temp<=0)
	{
		report=report+prdType + "??????\n";
		return false;
	}
	if((temp<min)||(temp>max))
	{
		report=report+prdType+"????"+min+"?????"+max+"?\n";
		return false;
	}
	if(s.length!=(""+temp).length)
	{
		report=report+prdType + "???????\n";
		return false;
	}
	return true;
}

function isCname(s,canNull,minl,maxl,nameType)	//13.Is the Chinese name valid?
{
	if(isEmpty(s)){
		if(canNull == false){
			report=report+nameType+"?????\n";
			return false;
		}else
			return true;
	}
	if((s.length > maxl)||(s.length < minl))
	{
		report=report+nameType+"????"+minl+"??????"+maxl+"??\n";
		return false;
	}
	if(!isAllChinese(s))
	{
		report=report+nameType+"???????\n";
		return false;
	}
	return true;
}

function isPinyin(s,canNull,minl,maxl,pyType)	//14.Is the Pinyin valid?
{
	if(isEmpty(s)){
		if(canNull == false){
			report=report+pyType+"?????\n";
			return false;
		}else
			return true;
	}
	if((s.length > maxl)||(s.length < minl))
	{
		report=report+pyType+"????"+minl+"??????"+maxl+"??\n";
		return false;
	}
	if(!isAllEnglish(s))
	{
		report=report+pyType+"????????????(_)?\n";
		return false;
	}
	return true;
}
//for field check. by langerwang
function fieldCheck(fieldName,fieldValue,canNull,maxLength,minLength)
{
  if((!canNull) && (isEmpty(fieldValue)))
  {
    alert("'" + fieldName + "'?????");
    return false;
  }
  if(!isEmpty(minLength) && !isEmpty(maxLength))
  {
    if(fieldValue > maxLength || fieldValue < minLength)
    {
      alert("'" + fieldName + "'???" + minLength + "??" + maxLength + "????");
      return false;
    }
  }
  return true;
}
//for numbercheck. by langerwang
function isNumber(fieldValue)
{
	if(!isCharsInBag(fieldValue,"0123456789") || isEmpty(fieldValue))
	{
		return false;
	}
	return true;
}
//for numbercheck. by langerwang
function isNumberNull(fieldValue)
{
	if(!isCharsInBag(fieldValue,"-0123456789"))
		return false;
	if(fieldValue.indexOf("-") > 0)
	  return false;
	return true;
}
//for numbercheck. by langerwang
function isPositiveNumberInteger(fieldValue)
{
	if(!isCharsInBag(fieldValue,"0123456789"))
		return false;
	return true;
}

//for numbercheck. by langerwang
function isNumberic(fieldValue)
{
	var fieldValue = Trim(fieldValue);
	if(isEmpty(fieldValue))
	{
		return true;
	}
	if(!isCharsInBag(fieldValue,"0123456789.-"))
	{
		return false;
	}
	if(fieldValue.indexOf("-") > 0 || fieldValue.indexOf("--") > 0)
	{
	  return false;
	}
	if(fieldValue.indexOf("..") >= 0)
	{
	  return false;
	}
	var pt=/\d+/g;
	var arr = fieldValue.match(pt);
	if(arr == null || arr.length > 2)
	{
	  return false;
	}
	return true;
}

//for email check. by langerwang
function emailCheck(fieldValue,canNull)
{
	var i = 1;

	if(isEmpty(fieldValue))
	{
		if (canNull)
		  return true;
		else
		{
		  return false;
		}
	}

	if (hasSpace(fieldValue))
	{
		return false;
	}
	var pos1 = fieldValue.indexOf("@");
	var pos2 = fieldValue.indexOf(".");
	var pos3 = fieldValue.lastIndexOf("@");
	var pos4 = fieldValue.lastIndexOf(".");
	if ((pos1 <= 0)||(pos1 == fieldValue.length)||(pos2 <= 0)||(pos2 == fieldValue.length))
	{
		return false;
	}
	else
	{
		if( (pos1 == pos2 - 1) || (pos1 == pos2 + 1)
		  || ( pos1 != pos3 )  //find two @
		  || ( pos4 < pos3 ) ) //. should behind the '@'
		{
			return false;
		}
	}
	return true;
}
//for popup window. by langerwang
function popup(url,name,sizes)
{
  //var remote = window.open("",name);
  //if(remote != null)
    //remote.close();
  if(top.popwindow != null)
  {
    //alert("close");
    top.popwindow.close();
  }
  var remote = window.open(url,name, sizes);
  if(top.popwindow != null)
    top.popwindow = remote;

  //remote.focus();
  //return remote;
}
//for check null checkbox. by langerwang
function nullChecked(ids,count)
{
  //alert("1234");
  if(ids == null)
    return true;
  if(ids.length == null)
  {
    if(ids.checked)
      return false;
    else
      return true;
  }
  for(var i = 0;i < ids.length;i++)
  {
    if(ids[i].checked)
      return false;
  }
  return true;
}
//for can null select option by langerwang
function selectCheck(form)
{
  for(var i=0;i<form.length;i++)
  {
    if(form.elements[i].type == "select-one")
    {
      if(form.elements[i].value == "-1")
      {
        form.elements[i].disabled = true;
      }
    }
  }
  return;
}


//for check all checkbox. by langerwang
function allChecked(form)
{
  for(var i=0;i<form.length;i++)
  {
    if(form.elements[i].type == "checkbox" && form.elements[i].disabled != true)
    {
      form.elements[i].checked = true;
    }
  }
}

//for not checke all checkbox. by langerwang
function allCancel(form)
{
  for(var i=0;i<form.length;i++)
  {
    if(form.elements[i].type == "checkbox")
    {
      form.elements[i].checked = false;
    }
  }
}
// for trim a string by langerwang
function LTrim(s)
{
	s = "" + s;  //added by potato. It is useful when the param_s is not the string type.
	for(var i=0;i<s.length;i++)
		if(s.charAt(i)!=' ')
			return s.substring(i,s.length);
	return "";
}
function RTrim(s)
{
	for(var i=s.length-1;i>=0;i--)
		if(s.charAt(i)!=' ')
			return s.substring(0,i+1);
	return "";
}
function Trim(s)
{
  if(s != null)
  	return RTrim(LTrim(s));
}
//for check date. by langerwang
/*
function dateCheck(dateValue,pattern)
{
	var pt=/\d+/g;
	if(isEmpty(dateValue))
	{
	  return dateValue;
	}
	var arr = dateValue.match(pt);
	if(arr == null)
	{
	  return -1;
	}
	if(arr.length != 3)
	{
	  return -1;
	}
	var newval = new Date(arr[0],arr[1]-1,arr[2]);
	arr[0] = newval.getYear();
	arr[1] = newval.getMonth();
	arr[2] = newval.getDate();
	if(pattern == "yyyy-mm-dd")
	{
		dateValue = "" + arr[0] + "-" + (new Number(arr[1]) + new Number(1)) + "-" + arr[2];
	}
	else
	{
		dateValue = "" + arr[0] + "-" + (new Number(arr[1]) + new Number(1)) + "-" + arr[2];
	}
	return dateValue;
}
*/

/**
 * created by jianni
 */
function isNullOrEmpty(d)
{
	if(d==null)
		return true;

	d = Trim(d);
	if(d.length==0)
		return true;

	return false;
}


/**
 * constants defining
 */
//------------------------------------------------------------------------------
var ILLEGAL_DATE = -1;
var ILLEGAL_TIME = -2;
var ILLEGAL_PATTERN = -3;
var ILLEGAL_TARGETPATTERN = -4;
var DEFAULT_PATTERN = "MM-dd-yyyy";
var DEFAULT_TIMEPATTERN = "hh:mm:ss";
var DEFAULT_TARGETPATTERN = "yyyy-MM-dd";
var PATTERNLIB = new Array( 'yy-MM-dd','MM-dd-yyyy', 'dd-MM-yyyy', 'yyyy-MM-dd',
							'M-d-yyyy', 'd-M-yyyy', 'yyyy-M-d',
							'MM/dd/yyyy', 'dd/MM/yyyy', 'yyyy/MM/dd',
							'M/d/yyyy', 'd/M/yyyy', 'yyyy/M/d',
							'MM-dd-yy', 'dd-MM-yy', 'yy-MM-dd',
							'M-d-yy', 'd-M-yy', 'yy-M-d',
							'MM/dd/yy', 'dd/MM/yy', 'yy/MM/dd',
							'M/d/yy', 'd/M/yy', 'yy/M/d',
							'hh:mm', 'hh:mm:ss');
//-----------------------------------------------------------------------------

/**
 * created by jianni, only support ie5 and above
 */
function dateCheck(dateValue, targetpattern, pattern)
{
	//-----------------------------------------------------------------------
	//variant defining
	//-----------------------------------------------------------------------
	var arrDigit = new Array(3);
	var arr = new Array(3);
	var flag = 'N';

	//-----------------------------------------------------------------------
	//precondition of conversion between the original format and the target
	//-----------------------------------------------------------------------
	if(isNullOrEmpty(dateValue))
	{
		return "";
	}
	if(isNullOrEmpty(targetpattern))
		targetpattern = DEFAULT_TARGETPATTERN;
	targetpattern = Trim(targetpattern);

	for(i=0;i<PATTERNLIB.length;i++)
		if(targetpattern==PATTERNLIB[i])
			flag = 'Y';

	if(flag=='N')
		return ILLEGAL_TARGETPATTERN;

	//-----------------------------------------------------------------------
	//get real date
	//-----------------------------------------------------------------------
	var newval = toDate(dateValue, pattern);
	if(newval==null || (Number(newval.toString()) != NaN && (Number(newval.toString()) ==ILLEGAL_DATE || Number(newval.toString())==ILLEGAL_TIME)))
		return ILLEGAL_DATE;

	arr[0] = newval.getFullYear();
	arr[1] = new Number(newval.getMonth()) + new Number(1);
	arr[2] = newval.getDate();

	//-----------------------------------------------------------------------
	//preparing target date format
	//-----------------------------------------------------------------------
	var arrTargetPattern = targetpattern.match(/[a-zA-Z]+/g);
	var arrTargetSeparator = targetpattern.match(/[^a-zA-Z]+/g);
	if(arrTargetSeparator==null || arrTargetPattern==null)
		return ILLEGAL_TARGETPATTERN;

	//-----------------------------------------------------------------------
	//get target date format
	//-----------------------------------------------------------------------
	dateValue = "";
	for(i=0;i<arrTargetPattern.length;i++)
	{
		if(i!=0)
			dateValue = dateValue + arrTargetSeparator[i-1];
		switch(arrTargetPattern[i]){
			case 'yyyy':
			case 'yy':
				arrDigit[0] = arrTargetPattern[i].length;
				arr[0] = arr[0];//((arr[0]<1000 && arrDigit[0]>2) ? (1900 + arr[0]) : arr[0]);
				dateValue = dateValue + arr[0];
				break;
			case 'MM':
			case 'M':
				arrDigit[1] = arrTargetPattern[i].length;
				arr[1] = ((arr[1]<10 && arrDigit[1]>1) ? ('0' + arr[1]) : arr[1]);
				dateValue = dateValue + arr[1];
				break;
			case 'dd':
			case 'd':
				arrDigit[2] = arrTargetPattern[i].length;
				arr[2] = ((arr[2]<10 && arrDigit[2]>1) ? ('0' + arr[2]) : arr[2]);
				dateValue = dateValue + arr[2];
				break;
			default:
				return ILLEGAL_PATTERN;
		}
	}

	return dateValue;
}

/**
 *
 */
function getArrMatchedFormat(value, pattern)
{
	//-----------------------------------------------------------------------
	//variant defining
	//-----------------------------------------------------------------------
	var arrIndex = new Array(6);
	var arrTarget = new Array(0, 0, 0, 0, 0, 0);
	var flag1 = 'N';

	//-----------------------------------------------------------------------
	//if the pattern isn't in the pattern library, return illegal flag
	//-----------------------------------------------------------------------
	value = Trim(value);
	if(isNullOrEmpty(value))
		return null;

	pattern = Trim(pattern);
	if(isNullOrEmpty(pattern))
		return null;

	for(i=0;i<PATTERNLIB.length;i++)
		if(pattern==PATTERNLIB[i])
			flag1 = 'Y';

	if(flag1=='N')
		return null;

	//-----------------------------------------------------------------------
	//get pattern data and seperator array
	//-----------------------------------------------------------------------
	var arrPattern = pattern.match(/[a-zA-Z]+/g);
	var arrSeparator = pattern.match(/[^a-zA-Z]+/g);
	if(arrSeparator==null || arrPattern==null)
		return null;

	//-----------------------------------------------------------------------
	//get value seperator array, compare the arrSeparator with the
	//arrSeparator0
	//-----------------------------------------------------------------------
	var arrSeparator0 = value.match(/\D+/g);
	if(arrSeparator0==null)
		return null;

	for(i=0;i<arrSeparator0.length;i++)
		if(arrSeparator0[i]!=arrSeparator[i])
			return null;

	for(i=0;i<arrPattern.length;i++)
	{
		switch(arrPattern[i]){
			case 'yyyy':
			case 'yy':
				arrIndex[i] = '0';
				break;
			case 'MM':
			case 'M':
				arrIndex[i] = '1';
				break;
			case 'dd':
			case 'd':
				arrIndex[i] = '2';
				break;
			case 'hh':
				arrIndex[i] = '3';
				break;
			case 'mm':
				arrIndex[i] = '4';
				break;
			case 'ss':
				arrIndex[i] = '5';
				break;
			default:
				return null;
		}
	}


	//-----------------------------------------------------------------------
	//get original date array
	//-----------------------------------------------------------------------
	var arrOriginal = value.match(/\d+/g);
	if(arrOriginal == null)
		return null;

	for(i=0;i<arrOriginal.length;i++)
		arrTarget[arrIndex[i]] = arrOriginal[i];
	return arrTarget;
}

/**
 * Pattern we received may be like this 'yyyy-MM-dd hh:mm:ss', so the first thing
 * is spliting it, and then handle apart. Assume the seperator for date and time
 * always be blank and the former is date.
 */
function toDate(dateValue, pattern)
{
	dateValue = Trim(dateValue);
	if(pattern != null)
	  pattern = Trim(pattern);
	else
	  pattern = "yyyy-MM-dd";
	var arrDate = null;
	var arrTime = null;

	//-----------------------------------------------------------------------
	//get divide date value and pattern which represent date
	//-----------------------------------------------------------------------
	var indexV = dateValue.indexOf(" ");
	var indexP = pattern.indexOf(" ");
	var dValue = dateValue;
	var dPattern = pattern;
	if(indexV!=-1 && indexP!=-1)
	{
		dValue = Trim(dateValue.substring(0, indexV));
		dPattern = Trim(pattern.substring(0, indexP));
	}
	arrDate = getArrMatchedFormat(dValue, dPattern);

	//-----------------------------------------------------------------------
	//get divide date value and pattern which represent time
	//-----------------------------------------------------------------------
	if(indexV!=-1 && indexP!=-1)
	{
		tValue = Trim(dateValue.substring(indexV));
		tPattern = Trim(pattern.substring(indexP));
		arrTime = getArrMatchedFormat(tValue, tPattern);
	}

	//-----------------------------------------------------------------------
	//combine date and time which from getArrMatchedFormat function
	//-----------------------------------------------------------------------
	var arrTarget = new Array("0", "0", "0", "0", "0", "0");
	var arr = new Array("0", "0", "0", "0", "0", "0");

	if(arrDate!=null)
	{
		arrTarget[0] = arrDate[0];
		arrTarget[1] = arrDate[1];
		arrTarget[2] = arrDate[2];
	}

	if(arrTime!=null)
	{
		arrTarget[3] = arrTime[3];
		arrTarget[4] = arrTime[4];
		arrTarget[5] = arrTime[5];
	}

	//-----------------------------------------------------------------------
	//get real date
	//-----------------------------------------------------------------------
	var newval = new Date(	arrTarget[0], arrTarget[1]-1, arrTarget[2],
							arrTarget[3], arrTarget[4], arrTarget[5]);
	arr[0] = newval.getYear();
	arr[1] = new Number(newval.getMonth()) + new Number(1);
	arr[2] = newval.getDate();
	arr[3] = newval.getHours();
	arr[4] = newval.getMinutes();
	arr[5] = newval.getSeconds();

	//-----------------------------------------------------------------------
	//ensure the date value is correct
	//-----------------------------------------------------------------------
	if(	new Number(arr[1])-new Number(arrTarget[1])!=0||
		new Number(arr[2])-new Number(arrTarget[2])!=0)
		return ILLEGAL_DATE;

	if(	new Number(arr[3])-new Number(arrTarget[3])!=0||
		new Number(arr[4])-new Number(arrTarget[4])!=0||
		new Number(arr[5])-new Number(arrTarget[5])!=0)
		return ILLEGAL_TIME;

	return newval;

}

function emptyToNull(frm)
{
	for(i=0;frm.elements[i]!=null;i++)
	{
		var elem = frm.elements[i];
		if(elem.type=='text' || elem.type=='textarea' || elem.command == "emptytonull")
			if(elem.value==null || Trim(elem.value).length==0)
				elem.value = " ";

		if(elem.type=='select-one')
		{
			if(elem.value==null || elem.value==-1)
			{
				opt = elem.options;
				for(var j = 0;j < opt.length;j++)
        {
          if(opt[j].value == -1)
            opt[j].value = " ";
        }
      }
    }
	}
}

function checkNull(frm)
{
	for(i=0;frm.elements[i]!=null;i++){
		var elem = frm.elements[i];
		if(elem.type=='text' || elem.type=='textarea' || elem.type=='password')
			if(elem.isnull!=null && Trim(elem.isnull)=="N" &&
				(elem.value==null || Trim(elem.value).length==0))
			{
				var bb = elem.label.replace("\\'","'");
				alert(bb);
				elem.focus();
				return false;
			}
		if(elem.type=='select-one' && elem.isnull!=null && Trim(elem.isnull)=="N")
		{
			if(elem.value==null || elem.value == "-1" || elem.value==-1 || Trim(elem.value).length==0)
			{
				var bb = elem.label.replace("\\'","'");
				alert(bb);
				return false;
			}
		}
	}
	return true;
}
function checkNum(frm)
{
	for(var i=0;frm.elements[i]!=null;i++){
		var elem = frm.elements[i];
		if(elem.type=='text' || elem.type=='hidden')
		if(elem.check!=null && elem.msg!=null && elem.value!="")
		{
			if (elem.check=="positive" && elem.value<= 0)
			{
				alert(elem.msg);
				return false;
			}
			if (elem.check=="nonegative" && elem.value< 0)
			{
				alert(elem.msg);
				return false;
			}
			if (elem.check=="lenientpositive" && elem.value< 0)
			{
				alert(elem.msg);
				return false;
			}
			if (elem.check=="lenientpositive" && elem.value== 0 && elem.msg1!=null)
			{
				if(!confirm(elem.msg1))
				return false;
			}
		}
	}
	return true;
}
/**
 * replace the tag 'baseTag'+i with obj in sourceString.
 */
function replaceTag(sourceString,obj,baseTag)
{
  if(obj!=null)
  {
    for(var i = 0;i < obj.length;i++)
    {
      var tag = baseTag+(i+1);
      if(obj[i]!=null)
      {
      	while(sourceString.match(tag))
      	{
          sourceString = sourceString.replace(tag,obj[i]);
        }
      }
    }
  }
  return sourceString;
}

//Check if an input is a positive integer. --by David
function IsPosInteger(value)
{
  inputStr = "" + value;

  if( Number(inputStr) == 0 )  return false;

  for (var i = 0; i < inputStr.length; i++)
  {
    var oneChar = inputStr.charAt(i);
    if (i == 0 && oneChar == "+")  continue;
    if (oneChar < "0" || oneChar > "9") return false;
  }
  return true;
}

/**
 * replaceString()
 */
function replaceString(sourceString,baseTag,obj1,obj2,obj3,obj4)
{
  if(obj1!=null)
    obj1=""+obj1;
  if(obj2!=null)  
    obj2=""+obj2;
  if(obj3!=null)
    obj3=""+obj3;
  if(obj4!=null)
    obj4=""+obj4;
  
  var str = sourceString;
  if(baseTag == null)
  {
    baseTag = "%s";
  }
  
  //for replaceString method to filtrate the 'font' format;by andyyang 2003/01/20
  if(obj1!=null && bFiltrateParam && Trim(obj1).indexOf("</font>")>0)
  {
    obj1 = obj1.substring(obj1.indexOf(">")+1,obj1.lastIndexOf("<")-1);
  }
  if(obj2!=null && bFiltrateParam && Trim(obj2).indexOf("</font>")>0)
  {
    obj2 = obj2.substring(obj2.indexOf(">")+1,obj2.lastIndexOf("<")-1);
  }
  if(obj3!=null && bFiltrateParam && Trim(obj3).indexOf("</font>")>0)
  {
    obj3 = obj3.substring(obj3.indexOf(">")+1,obj3.lastIndexOf("<")-1);
  }
  if(obj4!=null && bFiltrateParam && Trim(obj4).indexOf("</font>")>0)
  {
    obj4 = obj4.substring(obj4.indexOf(">")+1,obj4.lastIndexOf("<")-1);
  }
  if(obj1 != null)
    str = str.replace(baseTag + 1,obj1);
  if(obj2 != null)
    str = str.replace(baseTag + 2,obj2);
  if(obj3 != null)
    str = str.replace(baseTag + 3,obj3);
  if(obj4 != null)
    str = str.replace(baseTag + 4,obj4);
  
  return str;
}


function timeCheck(timeval,targetpattern,srcpattern)
{
  if(isNullOrEmpty(timeval))
    return " ";
  var timeExp1 = /^\d{1,2}:\d{1,2}:\d{1,2}$/;
  var timeExp2 = /^\d{1,2}:\d{1,2}$/;
  if(timeExp1.test(timeval))
    return timeval;
  else if(timeExp2.test(timeval))
    return timeval + ":00"
  else
    return ILLEGAL_TIME;
}

function toTime(timeval,pattern)
{
  return;
}



/**
 * replace the tag 'baseTag'+i with obj in sourceString.
 */
function replaceTag(sourceString,obj,baseTag)
{
  if(obj!=null)
  {
    for(var i = 0;i < obj.length;i++)
    {
      var tag = baseTag+(i+1);
      if(obj[i]!=null)
      {
      	while(sourceString.match(tag))
      	{
          sourceString = sourceString.replace(tag,obj[i]);
        }
      }
    }
  }
  return sourceString;
}

//Check if an input is a positive integer. --by David
function IsPosInteger(value)
{
  inputStr = "" + value;

  if( Number(inputStr) == 0 )  return false;

  for (var i = 0; i < inputStr.length; i++)
  {
    var oneChar = inputStr.charAt(i);
    if (i == 0 && oneChar == "+")  continue;
    if (oneChar < "0" || oneChar > "9") return false;
  }
  return true;
}

function numberRound(value,pattern)
{
  var ret;
  if(isEmpty(pattern) || isEmpty(value))
    return value;
  if(pattern.indexOf(".") == -1)
    return value;
  var scale = Number(pattern.length - pattern.indexOf(".")-1);
  if(pattern.indexOf("%") != -1)
    scale += 1;
   if(value<0){
  	ret = -1*Round(-1*value,scale);
  }else{
		ret = Round(value,scale);
	}
  return Number(ret);
}

function Round(value0, number0)
{
    var strValue=numberToString(value0); 
    var dot=(""+strValue).indexOf(".");
    if(dot<0)
      return strValue;
    var strInt=(""+strValue).substring(0,dot);                //\ufffd\ufffd\ufffd\ufffd\ufffd \ufffd\ufffd25
    var strfloat=(""+strValue).substring(dot+1);  
    var len0=(""+strfloat).length-(""+Number(strfloat)).length;
    if((""+strfloat).length>number0)
    {
        var strend=(""+strfloat).substring(number0,number0+1);        //\ufffd\ufffd\u04aa\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\u04bb\u03bb    \ufffd\ufffd5
       strfloat= (""+strfloat).substring(0,number0)   ;
       if(Number(strfloat)==0){                                      //\ufffd\ufffd\ufffd\ufffd.00\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd 8547.008
      var len2=(""+(strfloat)).length;
      }else{
       var len2=(""+Number(strfloat)).length;
      }
       if(Number(strend)>=5)
       {
        strfloat=""+(Number(strfloat)+1);
        var len3=(""+Number(strfloat)).length;
        if(len0>0 && len3!=len2)
        len0=len0-1;
        }
       if((""+strfloat).length>number0)                                  //\ufffd\ufffd\ufffd\u0421\ufffd\ufffd\ufffd\ufffd\u043d\ufffd\u03bb(\ufffd\ufffd\ufffd\ufffd\ufffd\ufffd)
       {
           strInt=Number(strInt)+1;
           strfloat=(""+strfloat).substring(1,number0+1);
       }
    }
    strfloat="0000000000000".substring(0,len0)+Number(strfloat)+"00000000000000000000";
    
    strfloat=strfloat.substring(0,number0);
   return Number(strInt)+"."+strfloat;
}

function checkNumber(frm)
{
	for(i=0;frm.elements[i]!=null;i++)
	{
		var elem = frm.elements[i];
		if(elem.type=='text' || elem.type=='hidden')
		{
			if(elem.datatype != null && Trim(elem.datatype)=="number" && Trim(elem.value).length != 0 && elem.dataformat != null)
			{
         elem.value = numberRound(numberToString(elem.value),elem.dataformat);
			}
	  }
	}
	return true;
}
function numberToString(val)
{
  //alert("function " + val);
  if(isEmpty(val) || val == "NaN") return "";
  if(val.toString().length == 0 || (val.toString().indexOf("e") == -1 && val.toString().indexOf("E") == -1))
    return val;
  var valo = "";
  if(val.toString().indexOf("-") == 0 || val.toString().indexOf("+") == 0)
  {
    valo = val.substring(0,1);
    if(valo == "+")
      valo == "";
    val = val.substring(1);
  }
  var str = val.toString();
  var pos = -1;
  if(str.indexOf("e") != -1)
    pos = str.indexOf("e");
  if(str.indexOf("E") != -1)
    pos = str.indexOf("E");
  var exp = Number(str.substring(pos + 1));
  //alert(exp);
  str = str.substring(0,pos);

  if(str.indexOf(".") == -1)
  {
    if(Number(exp) > 0)
    {
      for(var t = 0;t < exp;t++)
        str += "0";
      return valo + str;
    }
    else
     return valo + str.substring(0,str.length + Number(exp)) + str.substring(Number(exp)+1);
  }


  //alert("function str " + str);
  var part1 = str.substring(0,str.indexOf("."));
  var part2 = str.substring(str.indexOf(".") + 1,pos)
  //alert("function part1 " + part1);
  //alert("function part2 " + part2);
  //alert("function exp " + exp);

  if(Number(exp) >=0)
  {
    while(part2.length - exp < 0)
      part2 +="0";
    if(part2.length - exp > 0)
      part2 = part2.substring(0,exp) + "." + part2.substring(exp);
  }
  else
  {
    while(part1.length + exp < 0)
      part1 = "0" + part1;
    if(part1.length + exp == 0)
      part1 = "0." + part1;
    else if(part1.length + exp > 0)
      part1 = part1.substring(0,part1.length + exp) + "." + part1.substring(part1.length + exp);
  }
  //alert("function part1 " + part1);
  //alert("function part2 " + part2);
  return valo + part1 + part2;
}

function getLength(str)
{
  if(str == null)
    return 0;
  len = 0;
  str = str.toString();
  for (i=0;i<str.length;++i)
  {
    if ((str.charCodeAt(i) >= 0) && (str.charCodeAt(i) <= 255))
    	len += 1;
    else
      len += 2;
  }
  return len;
}
//add element to form
function addElementToForm(frm,ele_name,ele_value,opt,ele_type)
{
  var addn = document.all.item(ele_name);
  if(opt != null && opt == true && addn != null)
  {
    addn.value = ele_value;
    return;
  }
  var ele = document.createElement("INPUT");
  ele.type = "hidden";
  ele.name = ele_name;
  ele.value = ele_value;
  frm.appendChild(ele);
}
//for javascript debug
function frmToString(frm)
{
  var str = "\tname\t\t\ttype\t\t\tvalue\t\t";
  for(var i = 0;i < frm.elements.length;i++)
  {
    var elem = frm.elements[i];
    str += "\n" + i + "\t" + elem.name;
    if(getLength(elem.name)%8 < 4)
      str += "  ";
    var nm = elem.name.toString();
    if(getLength(nm) < 24)
      str +="\t";
    if(getLength(nm) <= 16)
      str +="\t";
    if(getLength(nm) < 8)
      str +="\t";
    str += elem.type;
    if(getLength(elem.type)%8 < 4)
      str += "  ";
    var tp = elem.type.toString();
    if(getLength(tp) < 24)
      str +="\t";
    if(getLength(tp) < 16)
      str +="\t";
    if(getLength(tp) < 8)
      str +="\t";
    str += elem.value;
  }
  return str;
}
function allIsEnglish(val)
{
  if(val == null)
    return true;
  for(var i=0;i<val.length;i++)
    if(val.charCodeAt(i) > 255 || val.charCodeAt(i) < 0)
      return false;
  return true;
}
var isRunning = false;
document.attachEvent("onmousedown",dosth);
function dosth()
{
  if(isRunning)
  {
    //alert("it's busy!");
    window.event.srcElement.onclick="";
    if(window.event.srcElement.parentNode!=null)
      window.event.srcElement.parentNode.onclick="";
    if(window.event.srcElement.parentNode!=null && window.event.srcElement.parentNode.parentNode!=null)
      window.event.srcElement.parentNode.parentNode.onclick="";
    window.event.returnValue = false;  
    window.event.returnValue = false;
  }
}
function redirtoinfo(urlrela,trantype,tranid,fromurl,flag)
{
  if (trantype=='1')
    window.location.href=urlrela+"ordermanagement/oe/SalesOrderInfo.jsp?orderid="+tranid;
  if (trantype=='2')
    window.location.href=urlrela+"iwm/TransferInfo.jsp?transferid="+tranid;
  if (trantype=='3')
    window.location.href=urlrela+"iwm/SelfUseSlipInfo.jsp?selfuseslipid="+tranid;
  if (trantype=='4')
  {
    if (arguments[3]!=null&&arguments[3]=='pur' && isEmpty(flag))
      window.location.href=urlrela+"purchasemanagement/gr/PurchaseReturningInfo.jsp?returnid="+tranid + "&from=" + fromurl;
    else if(isEmpty(flag))
      window.location.href=urlrela+"ordermanagement/ofm/SalesReturningInfo.jsp?returnid="+tranid + "&from=" + fromurl;
    else if(flag=="pr")   
    {
      window.location.href=urlrela+"purchasemanagement/gr/PurchaseReturningInfo.jsp?returnid="+tranid + "&from=" + fromurl;
    }
    else 
      window.location.href=urlrela+"ordermanagement/ofm/SalesReturningInfo.jsp?returnid="+tranid + "&from=" + fromurl;
  }
  if (trantype=='6')
    window.location.href=urlrela+"iwm/AdjustmentInfo.jsp?invadjustmentid="+tranid;
  if (trantype=='7')
    window.location.href=urlrela+"iwm/CheckinSlipInfo.jsp?checkinslipid="+tranid;
  if (trantype=='8')
    window.location.href=urlrela+"iwm/PickSlipInfo.jsp?pickslipid="+tranid;
  if (trantype=='9')
    window.location.href=urlrela+"purchasemanagement/gr/ReceiptInfo.jsp?receiptid="+tranid;
  if (trantype=='10')
    window.location.href=urlrela+"ordermanagement/ofm/DeliveryInfo.jsp?deliveryid="+tranid;
  if (trantype=='11')
    window.location.href=urlrela+"fim/SalesInvoiceInfo.jsp?invoiceid="+tranid;
  if (trantype=='12')
    window.location.href=urlrela+"fim/ARAdjustmentInfo.jsp?arapadjustmentid="+tranid;
  if (trantype=='14')
    if(fromurl == null || fromurl == '')
      window.location.href=urlrela+"salesmanagement/van/VANInfoFrm.jsp?from=pickslipsummary&vansalesslipid="+tranid;
    else
      window.location.href=urlrela+"salesmanagement/van/VANInfoFrm.jsp?from=" + fromurl + "&vansalesslipid="+tranid;
  if (trantype=='15')
    if(fromurl == null || fromurl == '')
      window.location.href=urlrela+"salesmanagement/van/VANInfoFrm.jsp?from=pickslipsummary&vansalesslipid="+tranid;
    else
      window.location.href=urlrela+"salesmanagement/van/VANInfoFrm.jsp?from=" + fromurl + "&vansalesslipid="+tranid;
  if (trantype=='22')
    window.location.href=urlrela+"iwm/InvWorkOrderFrm.jsp?flag=view&workorderid="+tranid;
  if (trantype=='37')
    window.location.href=urlrela+"iwm/InvWorkOrderFrm.jsp?trantype=37&flag=view&workorderid="+tranid;
  if (trantype=='40')
    window.location.href=urlrela+"iwm/InvWorkOrderFrm.jsp?trantype=40&flag=view&workorderid="+tranid;
  if (trantype=='41')
    window.location.href=urlrela+"iwm/InvWorkOrderFrm.jsp?trantype=41&flag=view&workorderid="+tranid;
  if (trantype=='42')
    window.location.href=urlrela+"iwm/InvWorkOrderFrm.jsp?trantype=42&flag=view&workorderid="+tranid;
  if (trantype=='43')
    window.location.href=urlrela+"iwm/InvWorkOrderFrm.jsp?trantype=43&flag=view&workorderid="+tranid;
}

function mergeURL(a_URL, dic)
{
  if(a_URL==null)
    return "";
  var keys = (new VBArray(dic.Keys())).toArray();
  if(keys.length==0)
  {
  	return a_URL;
  }
  if(a_URL.indexOf("?")==-1)
  {
    a_URL = a_URL+"?";
  }
  else
  {
	a_URL = a_URL+"&";
  } 
  for(var i=0;i<keys.length;i++)
  {
    a_URL =a_URL+keys[i]+"="+dic.Item(keys[i])+"&";
  }     
  if(a_URL.charAt(a_URL.length-1)=='&')
  {
  	a_URL = a_URL.substring(0,a_URL.length-1);
  }
  if(a_URL.charAt(a_URL.length-1)=='?')
  {
  	a_URL = a_URL.substring(0,a_URL.length-1);
  }
  return a_URL;
}  
//by using this function, total frm's action can be locked. added by andyyang,9/2/2004 2:17PM
function setIsRunning(thisObj)
{
  isRunning = true;
  if(thisObj.parent.up!=null) thisObj.parent.up.isRunning = true;
  if(thisObj.parent.down!=null) thisObj.parent.down.isRunning = true;
}

//?????????


function selectText(obj) {
    var range = obj.createTextRange()
    range.moveStart("character", 0);
    range.select();
}